package com.xbongbong.paas.model.impl;

import com.xbongbong.paas.domain.dao.PaasFormPaasSubDataDao;
import com.xbongbong.paas.domain.entity.PaasFormSubDataEntity;
import com.xbongbong.paas.elasticsearch.model.impl.PaasFormSubDataEsModelImpl;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.model.PaasFormPaasSubDataModel;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.pro.domain.entity.UpdateDataEntity;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.SubFormTableNameConstant;
import org.elasticsearch.action.support.WriteRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 *
 * @author 魏荣杰
 * @date 2020/9/4 11:29
 * @since
 * @version
 */
@Service("paasFormPaasSubDataModel")
public class PaasFormPaasSubDataModelImpl implements PaasFormPaasSubDataModel {

    @Resource
    private PaasFormPaasSubDataDao paasFormPaasSubDataDao;
    @Resource
    private PaasFormSubDataEsModelImpl paasFormSubDataEsModel;


    @Override
    public Integer insert(PaasFormSubDataEntity entity) throws XbbException {
        Integer insert = paasFormPaasSubDataDao.insert(entity, SubFormTableNameConstant.TB_PAAS_FORM_PAAS_SUB_DATA);
        paasFormSubDataEsModel.insert(entity, IndexTypeEnum.IDX_SUB_FORM_DATA);
        return insert;
    }

    @Override
    public Integer update(PaasFormSubDataEntity entity) throws XbbException {
        Integer update = paasFormPaasSubDataDao.update(entity, SubFormTableNameConstant.TB_PAAS_FORM_PAAS_SUB_DATA);
        paasFormSubDataEsModel.update(entity, IndexTypeEnum.IDX_SUB_FORM_DATA);
        return update;
    }


    @Override
    public List<PaasFormSubDataEntity> findEntitys(Map<String, Object> param) {
        return (List<PaasFormSubDataEntity>) paasFormPaasSubDataDao.findEntitys(param, SubFormTableNameConstant.TB_PAAS_FORM_PAAS_SUB_DATA);
    }

    @Override
    public List<PaasFormSubDataEntity> findEntitys4WorkOrderV2(Map<String, Object> param, String table) {
        return (List<PaasFormSubDataEntity>) paasFormPaasSubDataDao.findEntitys(param, table);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return paasFormPaasSubDataDao.getEntitysCount(param, SubFormTableNameConstant.TB_PAAS_FORM_PAAS_SUB_DATA);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public Integer insertBatch(List<PaasFormSubDataEntity> paasFormPaasSubDataEntities) throws XbbException {
        Integer integer = paasFormPaasSubDataDao.insertBatch(paasFormPaasSubDataEntities, SubFormTableNameConstant.TB_PAAS_FORM_PAAS_SUB_DATA);
        paasFormSubDataEsModel.insertBatch(paasFormPaasSubDataEntities, IndexTypeEnum.IDX_SUB_FORM_DATA);
        return integer;
    }

    @Override
    public Integer deleteBatch(String corpid, List<PaasFormSubDataEntity> entities, WriteRequest.RefreshPolicy refreshPolicy) throws XbbException {
        if (CollectionsUtil.isEmpty(entities)) {
            return 0;
        }
        List<Long> ids = new ArrayList<>();
        entities.forEach(item -> ids.add(item.getId()));
        Integer result = paasFormPaasSubDataDao.deleteById(ids, corpid, SubFormTableNameConstant.TB_PAAS_FORM_PAAS_SUB_DATA);

        paasFormSubDataEsModel.deleteBatch(corpid, entities, IndexTypeEnum.IDX_SUB_FORM_DATA, refreshPolicy, DelEnum.DELETE);
        return result;
    }


    @Override
    public Integer deleteByDataId(Long dataId, String corpid) throws XbbException {
        List<PaasFormSubDataEntity> entities = getByDataId(dataId, corpid);
        Integer result = paasFormPaasSubDataDao.deleteByDataId(dataId, corpid, SubFormTableNameConstant.TB_PAAS_FORM_PAAS_SUB_DATA);

        paasFormSubDataEsModel.deleteBatch(corpid, entities, IndexTypeEnum.IDX_SUB_FORM_DATA, WriteRequest.RefreshPolicy.NONE, DelEnum.DELETE_SUBFORM);
        return result;
    }

    @Override
    public List<PaasFormSubDataEntity> getByDataId(Long dataId, String corpid) {
        return (List<PaasFormSubDataEntity>) paasFormPaasSubDataDao.getByDataId(dataId, corpid, SubFormTableNameConstant.TB_PAAS_FORM_PAAS_SUB_DATA);
    }

    @Override
    public List<PaasFormSubDataEntity> getByDataIds(List<Long> dataIds, String corpid, DelEnum delEnum) {
        return (List<PaasFormSubDataEntity>) paasFormPaasSubDataDao.getByDataIds(dataIds, corpid, delEnum.getDel(), SubFormTableNameConstant.TB_PAAS_FORM_PAAS_SUB_DATA);
    }

    @Override
    public Integer deleteBatchByDataId(List<Long> list, String corpid, DelEnum delEnum) throws XbbException {
        List<PaasFormSubDataEntity> entities = getByDataIds(list, corpid, DelEnum.NORMAL);
        Integer integer = paasFormPaasSubDataDao.deleteBatchByDataId(list, corpid, SubFormTableNameConstant.TB_PAAS_FORM_PAAS_SUB_DATA, delEnum.getDel());

        paasFormSubDataEsModel.deleteBatch(corpid, entities, IndexTypeEnum.IDX_SUB_FORM_DATA, WriteRequest.RefreshPolicy.NONE, delEnum);
        return integer;
    }

    @Override
    public Integer updateBatch(List<UpdateDataEntity> entities, String corpid) throws XbbException {
        if (CollectionsUtil.isEmpty(entities)) {
            return 0;
        }
        Integer integer = paasFormPaasSubDataDao.updateBatch(entities, corpid, SubFormTableNameConstant.TB_PAAS_FORM_PAAS_SUB_DATA);
        if (!Objects.equals(0, integer)) {
            paasFormPaasSubDataDao.updateBatchTime(entities, corpid, SubFormTableNameConstant.TB_PAAS_FORM_PAAS_SUB_DATA);
            paasFormSubDataEsModel.updateBatch(entities, IndexTypeEnum.IDX_SUB_FORM_DATA, WriteRequest.RefreshPolicy.NONE);
        }
        return integer;
    }

    @Override
    public void restoreBatch(List<Long> dataIds, String corpid) throws XbbException {
        List<PaasFormSubDataEntity> entities = getByDataIds(dataIds, corpid, DelEnum.DELETE_SUBFORM);
        paasFormPaasSubDataDao.restoreBatch(dataIds, corpid, SubFormTableNameConstant.TB_PAAS_FORM_PAAS_SUB_DATA);
        paasFormSubDataEsModel.deleteBatch(corpid, entities, IndexTypeEnum.IDX_SUB_FORM_DATA, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
    }

    @Override
    public Integer updateDel(List<PaasFormSubDataEntity> updateList, String corpid, WriteRequest.RefreshPolicy refreshPolicy, DelEnum delEnum) throws XbbException {
        if (CollectionsUtil.isEmpty(updateList)) {
            return 0;
        }
        List<Long> ids = new ArrayList<>();
        for (PaasFormSubDataEntity v : updateList) {
            ids.add(v.getId());
        }
        Integer result = paasFormPaasSubDataDao.updateDelByIdList(ids, corpid, SubFormTableNameConstant.TB_PAAS_FORM_PAAS_SUB_DATA, delEnum.getDel());
        paasFormSubDataEsModel.deleteBatch(corpid, updateList, IndexTypeEnum.IDX_SUB_FORM_DATA, refreshPolicy, delEnum);
        return result;
    }

    @Override
    public Integer updateBatchByEntity(List<? extends PaasFormSubDataEntity> list, String corpid) throws XbbException {
        Integer integer = paasFormPaasSubDataDao.updateBatchByEntity(list, corpid, SubFormTableNameConstant.TB_PAAS_FORM_PAAS_SUB_DATA);
        paasFormSubDataEsModel.updateBatchByEntity(list, IndexTypeEnum.IDX_SUB_FORM_DATA, WriteRequest.RefreshPolicy.NONE);
        return integer;
    }

    /**
     * 获取paas子表单最大id
     *
     * @param params
     * @return
     * @throws XbbException
     */
    @Override
    public Long getMaxId(Map<String, Object> params) throws XbbException {
        return paasFormPaasSubDataDao.getMaxId(params, "tb_paas_form_paas_sub_data");
    }

    @Override
    public void updateSubFormData4WorkOrderV2(List<? extends PaasFormSubDataEntity> deleteList, List<? extends PaasFormSubDataEntity> insertList, List<UpdateDataEntity> updateList, List<? extends PaasFormSubDataEntity> updateDelList, String corpid, IndexTypeEnum indexTypeEnum) throws XbbException {
        String tableName;
        switch (indexTypeEnum) {
            case IDX_SERVICE_PROJECT_SUB:
                tableName = SubFormTableNameConstant.TB_WORK_ORDER_V2_SERVICE_PROJECT_SUB_DATA;
                break;
            case IDX_RECEIPT_ORDER_SUB:
                tableName = SubFormTableNameConstant.TB_WORK_ORDER_V2_RECEIPT_SUB_DATA;
                break;
            case IDX_WORK_ORDER_V2_SUB:
                tableName = SubFormTableNameConstant.TB_WORK_ORDER_V2_SUB_DATA;
                break;
            default:
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }


        if (!deleteList.isEmpty()) {
            List<Long> ids = new ArrayList<>();
            for (PaasFormSubDataEntity item : deleteList) {
                ids.add(item.getId());
            }
            paasFormPaasSubDataDao.deleteById(ids, corpid, tableName);
            paasFormSubDataEsModel.deleteBatch(corpid, deleteList, indexTypeEnum, WriteRequest.RefreshPolicy.NONE, DelEnum.DELETE);
        }
        if (!insertList.isEmpty()) {
            paasFormPaasSubDataDao.insertBatch(insertList, tableName);
            paasFormSubDataEsModel.insertBatch(insertList, indexTypeEnum);
        }
        if (!updateList.isEmpty()) {
//            StandardizeDataUtil.standardizeList(updateDataList);
            //批量更新子表单
            Integer result = paasFormPaasSubDataDao.updateBatch(updateList, corpid, tableName);
            if (Objects.equals(BasicConstant.ZERO, result)) {
                //批量更新数据更新时间
                paasFormPaasSubDataDao.updateBatchTime(updateList, corpid, tableName);
                //批量更新es
                paasFormSubDataEsModel.updateBatch(updateList, indexTypeEnum, WriteRequest.RefreshPolicy.NONE);
            }
        }
        if (!updateDelList.isEmpty()) {
            List<Long> ids = new ArrayList<>();
            for (PaasFormSubDataEntity v : updateDelList) {
                ids.add(v.getId());
            }
            paasFormPaasSubDataDao.updateDelByIdList(ids, corpid, tableName, DelEnum.NORMAL.getDel());
            paasFormSubDataEsModel.deleteBatch(corpid, updateDelList, indexTypeEnum, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
        }

    }
}
